from PCA import PCA
from FDA import FDA
from utils import get_aver, get_pc, get_cm
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

import sys
sys.path.append("..")

from linear_classifier.PLA  import PLA_single



# 初始样本集
c1 = [[1,2],[2,3],[3,3],[4,5],[5,5]]
c2 = [[1,0],[2,1],[3,1],[3,2],[5,3],[6,5]]
c = c1 + c2

print(
'''
作业二 题2.1
'''
)

# 获取主分量
pc = get_pc(get_cm(c))

# 获取样本均值
aver = get_aver(c)

# 绘制主分量
plt.plot([aver[0],aver[0]+5*pc[0][0],aver[0]-5*pc[0][0]], [aver[1],aver[1]+5*pc[1][0],aver[1]-5*pc[1][0]])

# 绘制样本点
for s in c1:
    plt.plot(s[0], s[1], "ob")    

for s in c2:
    plt.plot(s[0], s[1], "or")   

# 绘制样本均值 也即新坐标系原点
plt.plot(aver[0],aver[1],"og")
    

# 主成分分析获取降维结果
res = PCA(c)

del_x = pc[0][0]
del_y = pc[1][0]

# 投影点在原坐标系的坐标
new_c = []

# 绘制出每个样本点在主分量上的投影 也即降维后的样本点
for i,r in enumerate(res):
    l = r[0]
    if i < 5:
        color = "ob"
    else:
        color = "or" 
    x = aver[0] + l * del_x  / (del_x**2 + del_y**2)**(1/2)
    y = aver[1] + l * del_y  / (del_x**2 + del_y**2)**(1/2)
    plt.plot(x,y,color)
    plt.plot([c[i][0],x],[c[i][1],y],linestyle="dashed",color="gray")
    
    # 记录投影点在原坐标系的坐标
    new_c.append([x,y])
    


#  把x、y轴的刻度间隔设置为1，并存在变量里
x_major_locator=MultipleLocator(1)
y_major_locator=MultipleLocator(1)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)

# 设置坐标轴刻度范围
plt.xlim(0,8)
plt.ylim(0,6)

print("PCA的降维样本集: ")
print(res,end="\n\n")

print("PCA结果的主分量为: ")
print(pc,end="\n\n")

print("新坐标系原点在原坐标系上的坐标为: ")
print(aver,end="\n\n")

print("PCA结果在原坐标系上的坐标为: ")
print(new_c,end="\n\n")



plt.show()



print(
'''
作业二 题2.2
'''
)

# w1 = [ [1,3], [1,4], [3,0], [3,1] ]
# w2 = [ [3,6], [3,7], [5,5], [5,4] ]
# w3 = [ [8,5], [9,9], [9,5], [10,9] ]

c1 = [[1,2],[2,3],[3,3],[4,5],[5,5]]
c2 = [[1,0],[2,1],[3,1],[3,2],[5,3],[6,5]]
c = c1 + c2

# 绘制样本点
for s in c1:
    plt.plot(s[0], s[1], "ob")    

for s in c2:
    plt.plot(s[0], s[1], "or")   



fda, pc = FDA([c1,c2])


# 绘制主分量
plt.plot([0,pc[0][0]*20,-pc[0][0]*20], [0,pc[1][0]*20,-pc[1][0]*20])

del_x = pc[0][0]
del_y = pc[1][0]
new_c = []
# 绘制出每个样本点在主分量上的投影 也即降维后的样本点
for i,r in enumerate(fda):
    l = r[0]
    if i < 5:
        color = "ob"
    else:
        color = "or" 
    x = l * del_x  / (del_x**2 + del_y**2)**(1/2)
    y = l * del_y  / (del_x**2 + del_y**2)**(1/2)
    plt.plot(x,y,color)
    plt.plot([c[i][0],x],[c[i][1],y],linestyle="dashed",color="gray")
    
    # 记录投影点在原坐标系的坐标
    new_c.append([x,y])






#  把x、y轴的刻度间隔设置为1，并存在变量里
x_major_locator=MultipleLocator(1)
y_major_locator=MultipleLocator(1)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)

# 设置坐标轴刻度范围
plt.xlim(-1.5,8)
plt.ylim(-1.5,6)

print("FDA的降维样本集: ")
print(fda,end="\n\n")

print("FDA结果的主分量为: ")
print(pc,end="\n\n")

print("FDA结果在原坐标系上的坐标为: ")
print(new_c,end="\n\n")

pla =  PLA_single(new_c[:5],new_c[5:],[0,1,1],order=None)

print("降维样本使用感知器算法得到的准则函数权值向量为: ")

print(pla)

plt.plot([0,pla[1]*20,-pla[1]*20],[0,pla[2]*20,-pla[2]*20],color="red")


plt.show()
